package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.FieldValOptionVo;
import com.zb.entity.ZjNetworkFieldVal;
import com.zb.entity.ZjNetworkFieldValOption;
import com.zb.extend.NetworkFieldValExtend;
import com.zb.mapper.ZjNetworkFieldValMapper;
import com.zb.service.ZjNetworkFieldValOptionService;
import com.zb.service.ZjNetworkFieldValService;
import com.zb.vo.NetworkFieldValVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 网点字段值表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-18
 */
@Service
public class ZjNetworkFieldValServiceImpl extends ServiceImpl<ZjNetworkFieldValMapper, ZjNetworkFieldVal> implements ZjNetworkFieldValService {
    @Resource
    private ZjNetworkFieldValOptionService networkFieldValOptionService;

    // 保存或更改服务商字段值
    @Override
    public void save(Long custAccId, Long networkId, List<NetworkFieldValVo> fieldVals) {
        if (CollectionUtils.isEmpty(fieldVals)) {
            return;
        }
        List<ZjNetworkFieldVal> adds = new ArrayList<>();
        List<ZjNetworkFieldVal> updates = new ArrayList<>();
        ZjNetworkFieldVal fieldVal = null;
        List<ZjNetworkFieldValOption> addOpts = new ArrayList<>();
        ZjNetworkFieldValOption opt = null;
        List<Long> delOptIds = new ArrayList<>();
        for (NetworkFieldValVo vo : fieldVals) {
            fieldVal = new ZjNetworkFieldVal();
            BeanUtil.copyProperties(vo, fieldVal);
            if (null != fieldVal.getId()) {
                updates.add(fieldVal);
            } else {
                fieldVal.setId(IdWorker.getId());
                fieldVal.setCustAccId(custAccId);
                fieldVal.setNetworkId(networkId);
                fieldVal.setNetworkFieldId(vo.getFieldId());
                adds.add(fieldVal);
            }
            for (FieldValOptionVo optVo : vo.getOptVos()) {
                opt = new ZjNetworkFieldValOption();
                BeanUtil.copyProperties(optVo, opt);
                opt.setId(IdWorker.getId());
                opt.setCustAccId(custAccId);
                opt.setNetworkId(networkId);
                opt.setNetworkFieldValId(fieldVal.getId());
                addOpts.add(opt);
            }
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
        networkFieldValOptionService.deleteByNetworkId(networkId);
        if (CollectionUtil.isNotEmpty(addOpts)) networkFieldValOptionService.saveBatch(addOpts);
    }

    // 根据服务商字段ID查询关联
    @Override
    public List<Long> getByNetworkFieldIds(List<Long> networkFieldIds) {
        if (CollectionUtils.isEmpty(networkFieldIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("network_Field_Id");
        wrapper.in("network_Field_Id", networkFieldIds);
        return this.list(wrapper).stream().map(ZjNetworkFieldVal::getNetworkFieldId).distinct().toList();
    }

    // 根据服务商ID获取服务商字段值
    @Override
    public List<ZjNetworkFieldVal> getByNetworkId(Long networkId) {
        QueryWrapper<ZjNetworkFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_Field_Id", "type", "name");
        wrapper.eq("network_id", networkId);
        return this.list(wrapper);
    }

    // 根据服务商ID批量删除服务商自定义字段
    @Override
    public void deleteBatchByNetworkIds(List<Long> networkIds) {
        if (CollectionUtils.isEmpty(networkIds)) {
            return;
        }
        QueryWrapper<ZjNetworkFieldVal> wrapper = new QueryWrapper<>();
        wrapper.in("network_id", networkIds);
        this.remove(wrapper);
    }

    // 根据服务商ID获取服务商自定义字段
    @Override
    public List<NetworkFieldValExtend> getByNetworkIds(List<Long> networkIds) {
        if (CollectionUtil.isEmpty(networkIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_id", "network_field_id")
                .in("network_id", networkIds);
        List<ZjNetworkFieldVal> fieldValList = this.list(wrapper);
        if (CollectionUtil.isEmpty(fieldValList)) {
            return new ArrayList<>();
        }
        List<ZjNetworkFieldValOption> valOptions = networkFieldValOptionService.getByNetworkFieldValIds(fieldValList.stream().map(ZjNetworkFieldVal::getId).toList());
        return fieldValList.stream().map(fieldVal -> {
            NetworkFieldValExtend extend = new NetworkFieldValExtend();
            BeanUtil.copyProperties(fieldVal, extend);
            extend.setValues(valOptions.stream().filter(f -> Objects.equals(f.getNetworkFieldValId(), fieldVal.getId())).map(ZjNetworkFieldValOption::getOptValue).toList());
            return extend;
        }).toList();
    }
}
